package com.szh.easyexcel.service.impl;

import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON;
import com.szh.easyexcel.dto.StudentExcelDto;
import com.szh.easyexcel.mapper.StudentMapper;
import com.szh.easyexcel.model.Student;
import com.szh.easyexcel.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: SongZiHao
 * @date: 2023/5/27
 */
@Service
public class StudentServiceImpl implements StudentService {

    private static final Logger LOGGER = LoggerFactory.getLogger(StudentServiceImpl.class);

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 查询学生列表，将结果类型封装成写入Excel中的类型StudentExcelDto
     * @return List<StudentExcelDto>
     */
    @Override
    public List<StudentExcelDto> queryAllStudents() {
        LOGGER.info("queryAllStudents start....");
        List<Student> studentList = studentMapper.findAllStudents();
        LOGGER.info("查询学生列表 studentList ===> {}", JSON.toJSONString(studentList));
        List<StudentExcelDto> studentExcelList = ListUtils.newArrayList();
        for (Student student : studentList) {
            StudentExcelDto studentExcelDto = new StudentExcelDto();
            BeanUtils.copyProperties(student, studentExcelDto);

            studentExcelList.add(studentExcelDto);
        }
        LOGGER.info("转换成写入Excel中的数据集 studentExcelList ===> {}", JSON.toJSONString(studentExcelList));
        return studentExcelList;
    }
}
